public class Tile {
	public int x;
	public int y;
	protected Tileable[] tileable;
	public int size;
	public char label;
	public boolean passable;
	public Item item;
	public AreaEffect areaEffect;
	public Resource resource;

	public Tile(int _y, int _x, char _label) {
		this.x = _x;
		this.y = _y;
		this.label = _label;
		if (label == '.' || label == '|')
			passable = true;
		else
			passable = false;
		this.size = 0;
		this.tileable = new Tileable[2];
	}

	private void doubleSize() {
		int sz = this.tileable.length;
		Tileable[] newTb = new Tileable[sz * 2];
		for (int i = 0; i < this.size; i++)
			newTb[i] = this.tileable[i];
		this.tileable = newTb;
	}

	public void putTileable(Tileable _tileable) {
		this.tileable[size] = _tileable;
		// System.out.println("Tileable will be put into ("+this.x+","+this.y+").");
		this.tileable[size].setX(this.x);
		this.tileable[size].setY(this.y);
		// System.out.println("Tileable has been put into ("+tileable[size].getX()+","+tileable[size].getY()+").");

		int p = this.tileable[size].getType();
		switch (p) {
		case 1:
			this.setLabel('1');
			break;
		case 2:
			this.setLabel('2');
			break;
		case 3:
			this.setLabel('3');
			break;
		case 4:
			this.setLabel('4');
			break;
		case 5:
			this.setLabel('5');
			break;
		case 6:
			this.setLabel('6');
			break;
		default:
			this.setLabel('-');
			break;
		}
		this.size++;
		if (tileable.length == size)
			doubleSize();
	}

	public Tileable remTileable(int id) {
		boolean found = false;
		Tileable temp = new Tileable();
		for (int i = 0; i < size; i++) {
			if (tileable[i].getID() == id) {
				temp = tileable[i];
				int j = i;
				for (j = i; j < size - 1; j++) {
					tileable[j] = tileable[j + 1];
				}
				found = true;
				break;
			}
		}
		size--;
		if (found == true) {
			tileable[size] = null;
			int p = 0;
			if (size > 2)
				p = this.tileable[size - 1].getType();
			switch (p) {
			case 1:
				this.setLabel('1');
				break;
			case 2:
				this.setLabel('2');
				break;
			case 3:
				this.setLabel('3');
				break;
			case 4:
				this.setLabel('4');
				break;
			default:
				this.setLabel('-');
				break;
			}
		}
		return temp;
	}

	public void deleteItem() {
		this.item = null;
	}

	public void addItem(Item i) {
		this.item = i;
	}

	public Item getItem() {
		return this.item;
	}

	public boolean hasItem() {
		return this.item != null;
	}
	
	public void addAreaEffect(AreaEffect ae)
	{
		this.areaEffect=ae;
	}
	public boolean hasAreaEffect()
	{
		return this.areaEffect!=null;
	}
	public void addResource(Resource r)
	{
		this.resource=r;
	}
	public Resource getResource()
	{
		return this.resource;
	}
	public AreaEffect getAreaEffect() {
		return this.areaEffect;
	}
	public boolean hasResource()
	{
		return this.resource!=null;
	}
	

	public int getTileable() {
		if (this.size > 0)
			return this.tileable[size - 1].getType();
		else
			return -1;
	}
	
	public Unit getMinHPUnit()
	{
		Unit u=null;
		int HP=10000;
		for(int i=0;i<this.size;i++)
		{
		
			if(tileable[i].getType()==0 ||
			   tileable[i].getType()==1 ||
			   tileable[i].getType()==2 ||
			   tileable[i].getType()==3)
			   {
			   	
			   	if(tileable[i].getStatus()[6]<HP)
			   	{
			   		HP=tileable[i].getStatus()[6];
			   		u=(Unit)tileable[i];
			   	}
			   }
		}
		return u;
	}

	public int getSize() {
		return this.size;
	}

	public int getX() {
		return this.x;
	}

	public int getY() {
		return this.y;
	}

	public char getLabel() {
		return this.label;
	}

	public void setLabel(char _label) {
		this.label = _label;
	}

	public String toString() {
		String s = "";
		int p = this.tileable[size - 1].getType();
		s += "This Tile(" + this.x + "," + this.y + ") has " + this.size
				+ " tileables with ";
		switch (p) {
		case 1:
			s += "Explorer as head, whose ID is ID:"
					+ this.tileable[size - 1].getID() + ".";
			break;
		case 2:
			s += "Colonist as head, whose ID is ID:"
					+ this.tileable[size - 1].getID() + ".";
			break;
		case 3:
			s += "Melee as head, whose ID is ID:"
					+ this.tileable[size - 1].getID() + ".";
			break;
		case 4:
			s += "Range as head, whose ID is ID:"
					+ this.tileable[size - 1].getID() + ".";
			break;
		default:
			s += "No unit.";
			break;
		}
		return s;
	}
}
